using System.Collections;
using System.Collections.Generic;
using System.Xml.Serialization;
using UnityEngine;
using UnityEngine.UI;

public class Grinding : MonoBehaviour
{
    // Start is called before the first frame update
    public float health;
    public float dietime;
    Transform ttransform;
    private Animator animator;
    private Rigidbody2D myRigidbody;
    private bool firstattack;
    Image healthbar;
    float maxhealth;
    // private Vector2 tempvel;
    public void TakeDamage(float Damage)
    {
        if (firstattack == false)
        {
            firstattack = true;
            
            animator.SetTrigger("convert");

            animator.SetBool("angry", true);
            return;
        }
        health -= Damage;
        if (health <= 0)
        {
            myRigidbody.gravityScale = 10;
            animator.SetTrigger("die");
            Transform[] myTransforms = GetComponentsInChildren<Transform>();
            foreach (var child in myTransforms)
                if(child.gameObject.CompareTag("Enemy"))child.parent = null;
            Invoke("Killplayer", dietime);
        }
        
        //StartCoroutine(Stiff());
    }
    void Killplayer()
    {
    

        Destroy(gameObject);
    }
    void Start()
    {
        health *= 2.5f*EnemyData.healthratio;
        maxhealth = health;
        animator = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();
        ttransform = GetComponent<Transform>();
        firstattack = false;
        healthbar =transform.GetChild(0).GetChild(0).GetComponent<Image>();
    }

    // Update is called once per frame
    void Update()
    {
        healthbar.fillAmount = health /maxhealth;
    }
}

